<template>
    <div class="postPic_container">
        <span class="intro">
            选择你的青年大学习截图
        </span>
        <!-- 选择上传图片 -->
        <div class="SelectPic">
            <div class="selectPic">
                <el-upload
                    action="https://gitee.com/tyt_t/youth_study/tree/master/"
                    list-type="picture-card"
                    :headers="token"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove"
                    :auto-upload="false"
                    :data="resData"
                    ref="upload"
                    name="imgUrl"
                    :on-success="onSuccess"
                    :on-progress="onProcess"
                    :limit="1">
                    <i class="el-icon-plus"></i>
                </el-upload>    
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </div>
        </div>
        <!-- 上传按钮     -->
        <div class="btn">
            <el-button type="primary" class="save" @click="submit">保存</el-button>
            <router-link to="/login">
                <el-button type="primary" class="back">返回</el-button>
            </router-link> 
        </div>
    </div>    
</template>

<script>
    export default {
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                file: '',
                imgUrl: '',
                userId: '',
                uid: '',

                // 上传附带的额外参数userId
                resData: {
                    userId: window.localStorage["userId"]
                },
            };
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },

            // 点击保存按钮上传图片
            submit:function(res){
                this.$refs.upload.submit();
            },

            // 图片上传成功后，后台返回图片的路径
            onSuccess:function(res){
                console.log(res);
            }
        }
    }
</script>

<style scoped>
.postPic_container{
    /* background-color: #cccccc; */
    width: 100%;
    height: 100%;
}
.intro{
    font-size: 18px;

}
.SelectPic{
    background-color: rgb(216, 250, 250);
    width: 300px;
    height: 400px;
    border: solid #cccccc 2px;
    border-radius: 3%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate( -50%, -60% );
}
.selectPic{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate( -50%, -60% );
}
.save{
    position: absolute;
    top: 80%;
    left: 49%;
    transform: translate( -100%, -60% );
}
.back{
    position: absolute;
    top: 80%;
    left: 52%;
    transform: translate( -5%, -60% );
}
</style>
